import 'package:flutter/material.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/common/refresh_listview.dart';
import 'package:gzmall_app/goods/favorites_goods_item_widget.dart';
import 'package:gzmall_app/goods/goods_details_page.dart';
import 'package:gzmall_app/model/favorites.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/model/favorites.dart';

class FootPrintPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => FootPrintPageState();
}

class FootPrintPageState extends State<FootPrintPage> {
  double _itemWidth;

  List<Favorites> _products = [];

  int _curPage = 1;
  RefreshListView _refreshListView;
  bool _showEmpty = false;

  void init() {
    var size = MediaQuery.of(context).size;
    _itemWidth = (size.width - 16) / 3;
  }

  @override
  void initState() {
    super.initState();
    _getSpuTracks(1);
  }

  @override
  Widget build(BuildContext context) {
    init();
    _refreshListView = RefreshListView(
      enablePullUp: true,
      enablePullDown: true,
      onRefresh: _onRefresh,
      child: _buildGoodList(context),
      showEmpty: _showEmpty,
      emptyView: Container(
          width: double.infinity,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Image.asset(
                "images/no_browse.png",
                width: 100.0,
                height: 100.0,
              ),
              Padding(
                padding: const EdgeInsets.only(top: 10.0),
                child: Text(
                  '暂无浏览记录',
                  style: TextStyle(fontSize: 18.0, color: ColorCommon.black1),
                ),
              ),
            ],
          )),
    );
    return Scaffold(
      appBar: _createAppBar(),
      body: _refreshListView,
    );
  }

  _createAppBar() {
    return new AppBar(
      elevation: 0.0,
      title: new Text("我的足迹"),
      leading: new IconButton(
        icon: new Icon(Icons.arrow_back_ios),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
      actions: <Widget>[],
    );
  }

  Widget _buildGoodList(BuildContext context) {
    return _buildGirdList();
  }

  Widget _buildGirdList() {
    List<Widget> spuList = _products
        .map((item) => new InkWell(
              child: new FavoritesGoodsItemWidget(item?.spu, _itemWidth),
              onTap: () {
                Navigator.of(context).push(new MaterialPageRoute(builder: (_) {
                  return new GoodsDetailsPage(productId: item?.spu?.id);
                })).then((v) {
                  _getSpuTracks(1);
                });
              },
            ))
        .toList();
    return GridView.count(
      shrinkWrap: true,
      padding: const EdgeInsets.only(left: 4.0, right: 4.0, top: 4.0),
      crossAxisSpacing: 4.0,
      mainAxisSpacing: 4.0,
      controller: new ScrollController(keepScrollOffset: false),
      crossAxisCount: 3,
      childAspectRatio: 0.7,
      children: spuList,
    );
  }

  void _onRefresh(bool up) {
    if (up) {
      _getSpuTracks(1);
    } else {
      _getSpuTracks(_curPage + 1);
    }
  }

  _getSpuTracks(int page) async {
    bool isGetNew = page == 1;
    Map<String, String> params = {
      "page": page.toString(),
      "per": '12',
    };
    NetUtil.get(
        context,
        Api.spuTracks,
        (data, message) {
          if (!mounted) return;
          _curPage = page;
          var _dataList = (data as List)
              ?.map((e) => e == null ? null : Favorites.fromJson(e))
              ?.toList();
          setState(() {
            _showEmpty =
                isGetNew && (_dataList == null || _dataList.length <= 0);
            if (isGetNew) {
              _products = _dataList;
            } else {
              _products.addAll(_dataList);
            }
          });
          if (_refreshListView != null) {
            _refreshListView.refreshCompleted(isGetNew,
                hasMore: _dataList != null && _dataList.length >= Api.pageSize);
          }
        },
        params: params,
        errorCallBack: (e) {
          setState(() {
            _showEmpty =
                isGetNew && (_products == null || _products.length <= 0);
          });
          if (_refreshListView != null) {
            _refreshListView.refreshCompleted(isGetNew);
          }
        });
  }
}
